package com.karrim.server.io;

import com.karrim.server.io.abstr.AbstractParser;
import java.io.File;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.util.LinkedHashSet;

/**
 *
 * @author andrea.cantafio
 */
public class CSVParser extends AbstractParser {

    public CSVParser(String delimiter, String encloser, String EOF) {
        DELIMITER = delimiter;
        ENCLOSER = encloser;
        this.EOF = EOF;

        parsed = new LinkedHashSet<String[]>();
    }

    /**
     * The default behaviour of this method is to clear parsed data at EACH
     * call. Note that data will be cleared only if file exists and it is not a
     * directory
     *
     * @param source the source file
     * @return {@link LinkedHashSet} that contains parsed data
     * @see #parse(java.io.File, boolean)
     * @throws FileNotFoundException if file does not exists
     * @throws IllegalArgumentException if file is a directory
     * @throws IOException thrown if an IO error occurs while reading file
     */
    public LinkedHashSet<String[]> parse(File source) throws FileNotFoundException, IllegalArgumentException, IOException {
        return parse(source, false);
    }

    /**
     * Parses data contained in
     * <code>source</code> Note that data will be cleared only if file exists
     * and it is not a directory
     *
     * @param source the source file
     * @param appendData if true, read data will be appended to parsed data of a
     * precedent call to this method, otherwise parsed data will be deleted
     * @return {@link LinkedHashSet} that contains parsed data
     * @throws FileNotFoundException if file does not exists
     * @throws IllegalArgumentException if file is a directory
     * @throws IOException thrown if an IO error occurs while reading file
     */
    public LinkedHashSet<String[]> parse(File source, boolean appendData) throws FileNotFoundException, IllegalArgumentException, IOException {
        if (!source.exists()) {
            throw new FileNotFoundException();
        }
        if (source.isDirectory()) {
            throw new IllegalArgumentException();
        }
        if (!appendData) {
            parsed.clear();
        }
        content = load(source);
        String[] rows = content.split("\r\n");
        for (String row : rows) {
            if (EOF.equals(row)) {
                break;
            }
            String[] arr = row.split(DELIMITER);
            for (String value : arr) {
                if (value.startsWith(ENCLOSER)) {
                    value = value.substring(1);
                }
                if (value.endsWith(ENCLOSER)) {
                    value = value.substring(0, value.length() - 1);
                }
            }
            parsed.add(arr);
        }
        return parsed;
    }
    public final String DELIMITER;
    public final String ENCLOSER;
    public final String EOF;
    private File source;
    private String content;
    private LinkedHashSet<String[]> parsed;
}
